package db
{
	import com.adobe.serialization.json.JSONDecoder;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.formatters.DateFormatter;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;
	
	import spark.formatters.DateTimeFormatter;
	
	import vo.voBoard;
	import vo.voImgInfo;

	public class mwHandler
	{
		public var sUrl:String = "http://bangkok-k.com/";
		
		public var hs:HTTPService;
		
		public var df:DateTimeFormatter;
		
		
		public function mwHandler()
		{
			hs = new HTTPService();
			hs.url = sUrl + "bbs/flex_write_update_jt.php?bo_table=jutae";
			hs.resultFormat = "text";
			hs.method = "POST";
			hs.addEventListener(FaultEvent.FAULT, mwFault);
			
			df = new DateTimeFormatter();
			df.dateStyle = "short";
			df.timeStyle = "none";
			df.dateTimePattern = "YYYY:MM:DD";
				
		}
		
		public var fnList:Function;
		public var fnInsert:Function;
		public var fnUpdate:Function;
		
		/**
		 * get List.........
		 */
		public function mwList( fn:Function=null):void
		{
			fnList = fn;
			hs.url = sUrl + "bbs/flex_board.php?bo_table=jutae";
			hs.addEventListener(ResultEvent.RESULT, _mwListResponse);	
			hs.send();
		}		
		public function _mwListResponse( event:ResultEvent = null):void
		{
			var rawData:String = String(event.result);   				
			var decoder:JSONDecoder = new JSONDecoder(rawData, false);
			var resultObject:Array = (decoder.getValue() as Array);
			var ac:ArrayCollection = parseList(new ArrayCollection(resultObject));
			fnList(ac);
			hs.removeEventListener(ResultEvent.RESULT, _mwListResponse);
		}			
		
		/**
		 * Insert.........
		 */
		public function mwInsert(obj:voBoard, fn:Function=null):void
		{
			fnInsert = fn;
			hs.url = sUrl + "bbs/flex_write_update_jt.php?bo_table=jutae";
			obj.wr_num = '1';
			obj.wr_option = 'html1';
			obj.wr_datetime = getNow();
			hs.addEventListener(ResultEvent.RESULT, _mwInsertResponse);				
			hs.send(obj);
		}
		public function _mwInsertResponse( event:ResultEvent = null):void
		{
			var rawData:String = String(event.result);   				
			var decoder:JSONDecoder = new JSONDecoder(rawData, false);
			if(fnInsert != null){
				fnInsert();
			}
			
			hs.removeEventListener(ResultEvent.RESULT, _mwInsertResponse);
		}
		
		/**
		 * Update.........
		 */
		public function mwUpdate(obj:voBoard, fn:Function=null):void
		{
			fnUpdate = fn;
			hs.url = sUrl + "bbs/flex_write_update_jt.php?bo_table=jutae";
			obj.w = 'u';
			obj.wr_datetime = getNow();
			hs.addEventListener(ResultEvent.RESULT, _mwUpdateResponse);	
			hs.send(obj);
		}
		public function _mwUpdateResponse( event:ResultEvent = null):void
		{
			var rawData:String = String(event.result);   				
			var decoder:JSONDecoder = new JSONDecoder(rawData, false);
			if(fnUpdate != null){
				fnUpdate();
			}
			
			hs.removeEventListener(ResultEvent.RESULT, _mwUpdateResponse);
		}
		
		
		
		public function mwResult(e:ResultEvent):void 
		{
			Alert.show("[mwFault]->" + e.message.toString());
		}
		
		public function mwFault(e:FaultEvent):void 
		{
			Alert.show("[mwFault]->" + e.message.toString());
		}
	
		
		public function getNow():String
		{
			return df.format(new Date());
		}
		
		
		/**
		 * parsing............ start  
		 */
			[Bindable] public var acPaging:ArrayCollection;
			private function parseList(recArr:ArrayCollection):ArrayCollection
			{
				var retArr:ArrayCollection = new ArrayCollection();
				
				for each(var obj:Object in recArr){
					var retObj:voBoard = new voBoard();
					for(var field:String in obj) {
						
						if(field == '0') {							
							acPaging = new ArrayCollection(obj as Array);
							parsePaging(new ArrayCollection(obj as Array));
							return retArr;
						}
						
						if(retObj.hasOwnProperty(field)==false ) {
							continue;
						}
						
						

						if(field == "href") {
							retObj["href"] = String(obj[field]).replace("board.php", "flex_board.php");							
						}		
						else if(field ==  "wr_subject") {
							retObj["wr_subject"] = decodeURIComponent(obj["wr_subject"]);						
						}
						else if(field ==  "wr_datetime") {
							//2012-01-28 05:35
							retObj["wr_datetime"] = String(obj["wr_datetime"]).substring(0, 10);						
						}
						else if(field == "reply") {
							retObj["reply"] = String(obj["reply"]);
							if(String(obj["reply"]) == ''){								 
								retObj.reply_is = false;
							}
							else {
								retObj.reply_is = true;
							}
						}
						else {
							retObj[field] = obj[field];
						}					
					}
					retArr.addItem(retObj);				
				}
				return retArr;
			}
			
			
			private function parsePaging(recArr:ArrayCollection):void
			{
				var retArr:ArrayCollection = new ArrayCollection();
				
				for each(var obj:Object in recArr){
					var retObj:Object = new Object();
					for(var field:String in obj) {						
						if(field == "1") {				
							retObj["url"] = obj[field];
						} else if(field == "2") {
							retObj["label"] = obj[field];
						}
					}
					retArr.addItem(retObj);				
				}
				acPaging =  retArr;		
				
			}
		
		
		
		
		
	}
}